--POUR CONTROLE


define erp='erp_mli2.' ;
define outils='outils_mli2.' ;



--CREATE TABLE se_art_unite AS (SELECT * FROM &erp.ART_UNITE WHERE 1=2);



DELETE FROM  se_art_unite ;




INSERT INTO se_art_unite
 (SELECT Upper(id) unite_cod,
         0 enreg_id,
         Decode(UNITESUP,id,NULL,unitesup) unitesup_cod,
         'N' enreg_s,
         libelle unite_lib,
         NULL unite_lib2,
         Decode(&erp.isnumeric(typunite),1,typunite,0) precision_val,
         0+(RPad('0.',Decode(&erp.isnumeric(typunite),1,typunite,0)+1,'0')||'1') precisionarrondi_val,
         Decode(arrondi,'S','+','I','-','=') precisionarrondi_typ,
         Decode(unitesup,NULL,NULL,Decode(UNITESUP,id,NULL,1)) numersup_conv,
         Decode(unitesup,NULL,NULL,Decode(UNITESUP,id,NULL,RPad('1',convsup+1,'0'))) denomsup_conv
         
    FROM UV_PUNITES un
  ) ;

 UPDATE se_art_unite u SET unitesup_cod=null,numersup_conv=null,denomsup_conv=NULL
 WHERE NOT EXISTS (SELECT 1 FROM se_art_unite uu WHERE uu.unite_cod=u.unitesup_cod) ;


 UPDATE se_art_unite tf
 SET enreg_id = (select enreg_id FROM &erp.art_unite f WHERE f.unite_cod=tf.unite_cod ) ;



 UPDATE se_art_unite SET enreg_id=&outils.enreg_id.NEXTVAL WHERE enreg_id IS NULL OR enreg_id =0;


 CALL uvse_synchroverserp('unite_cod','ART_UNITE','O') ;

COMMIT;

SELECT &outils.stragg(source) source, unite_cod, enreg_id, unitesup_cod, enreg_s, unite_lib, unite_lib2, precision_val, precisionarrondi_val, precisionarrondi_typ, numersup_conv, denomsup_conv
FROM (
  SELECT 'uv' source ,g.* FROM se_ART_UNITE g
  UNION ALL
  SELECT 'se' source ,g.* FROM &erp.ART_UNITE g
  )
GROUP BY unite_cod, enreg_id, unitesup_cod, enreg_s, unite_lib, unite_lib2, precision_val, precisionarrondi_val, precisionarrondi_typ, numersup_conv, denomsup_conv
HAVING Count(1)!=2
ORDER BY unite_cod;